package windowFactoryPackage;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class InformationWindow extends WindowGenerator{
	Query q = new Query();
	//Declare window components
	private JPanel descriptionPanel, progInfoPanel, sourceCodePanel, detailsPanel, buttonPanel;
	private JScrollPane fullDescripPanel;
	private JButton reviewButton, liscenceButton, developerButton, downloadButton, sourceCodeButton, 
		writeReviewButton;
	private JLabel sourceCodeLabel;
	static JTextArea jta1 = new JTextArea("");
	static String [] IPHolderInfoArray = {"", "", ""};
	static String [] progInfoArray = {"", "", "", ""};
	static JLabel [] IPLabelArray = new JLabel[6];
	static JLabel [] progInfoLabelArray = new JLabel[8];
	Font f = new Font("Dialog", Font.BOLD+Font.ITALIC, 14);
	
	public InformationWindow(){
		//Set layout of parent JFrame
		setLayout(new BorderLayout());
		
		//Instantiate descriptionPanel -- Instantiate components and add to descriptionPanel
		descriptionPanel = new JPanel();
		descriptionPanel.setLayout(new BorderLayout());
		//jta1 = new JTextArea();
		jta1.setEditable(false);
		jta1.setLineWrap(true); //wraps text
		jta1.setWrapStyleWord(true);
		//jta1.setText("Microsoft Word\n\nMicrosoft Word 2010 offers the best of all worlds: enhanced features to create professional-quality documents, easier ways to work together with people, and almost-anywhere access to your files. Designed to give you the finest document-formatting tools, Word 2010 also helps you easily organize and write your documents more efficiently, and stay within reach so you can capture your best ideas whenever and wherever they occur.");
		fullDescripPanel = new JScrollPane(jta1);
		fullDescripPanel.setBorder(BorderFactory.createEtchedBorder());
		//fullDescripPanel.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		
		descriptionPanel.add(fullDescripPanel, BorderLayout.CENTER);
		
		//Instantiate progInfoPanel -- Populate JLabel array that holds query information about program
		//add array to progInfoPanel and all to panel
		progInfoPanel = new JPanel();
		progInfoPanel.setBorder(BorderFactory.createEtchedBorder());
		progInfoPanel.setLayout(new GridLayout(progInfoLabelArray.length/2, 2, 0, 20));
		progInfoLabelArray[0] = new JLabel("Release Date:  ");
		progInfoLabelArray[1] = new JLabel(progInfoArray[0]);
		progInfoLabelArray[2] = new JLabel("File Size:");
		progInfoLabelArray[3] = new JLabel(progInfoArray[1]);
		progInfoLabelArray[4] = new JLabel("Required OS:");
		progInfoLabelArray[5] = new JLabel(progInfoArray[2]);
		progInfoLabelArray[6] = new JLabel("Rating:");
		progInfoLabelArray[7] = new JLabel(progInfoArray[3]);
		
		for(int i = 0; i<progInfoLabelArray.length; i++){
			if(i%2==0)progInfoLabelArray[i].setFont(f);
			progInfoPanel.add(progInfoLabelArray[i]);
		}
		
		//add progInfoPanel to descriptionPanel.EAST
		descriptionPanel.add(progInfoPanel, BorderLayout.EAST);
		
		//Instantiate souceCodePanel -- add components and add panel to descriptionPanel.SOUTH
		sourceCodePanel = new JPanel();
		sourceCodePanel.setBorder(BorderFactory.createEtchedBorder());
		
		//0 is no source code, anything greater is considered sourceCode
		if(Query.sourceCode_idSearch > 0){
			sourceCodeLabel = new JLabel("Source code is availble for this program.");
			sourceCodePanel.add(sourceCodeLabel);
			sourceCodeButton = new JButton("View");
			sourceCodeButton.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					
					q.viewSourceCode();
					WindowGenerator.generateSourceCodeWindow();
				}
			});
			sourceCodePanel.add(sourceCodeButton);
		}else{
			sourceCodeLabel = new JLabel("Source Code is not availble for this program");
			sourceCodePanel.add(sourceCodeLabel);
		}
		descriptionPanel.add(sourceCodePanel, BorderLayout.SOUTH);
		
		//add descriptionPanel to parent JFrame
		add(descriptionPanel, BorderLayout.CENTER);
		
		//Instantiate detailsPanel -- populate IPLabel array from queries
		//add array to detailsPanel -- adds detailsPanel to parent JFrame.EAST
		detailsPanel = new JPanel();
		detailsPanel.setLayout(new GridLayout(1, IPLabelArray.length, 0, 20));
		detailsPanel.setBorder(BorderFactory.createEtchedBorder());
		IPLabelArray[0] = new JLabel("Developer Name:");
		IPLabelArray[1] = new JLabel(IPHolderInfoArray[0]);
		IPLabelArray[2] = new JLabel("Developer Website:");
		IPLabelArray[3] = new JLabel(IPHolderInfoArray[1]);
		IPLabelArray[4] = new JLabel("Developer Phone:");
		IPLabelArray[5] = new JLabel(IPHolderInfoArray[2]);
		for(int i = 0; i<IPLabelArray.length; i++){
			if(i%2==0)IPLabelArray[i].setFont(f);
			detailsPanel.add(IPLabelArray[i]);
		}
		add(detailsPanel, BorderLayout.NORTH);
		
		
		//Instantiate and add button components to panel -- add buttonPanel to parent JFrame.SOUTH
		buttonPanel = new JPanel();
		buttonPanel.setBorder(BorderFactory.createEtchedBorder());
		reviewButton = new JButton("Read Reviews");
		reviewButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				q.infoWindowProgReview();
				WindowGenerator.generateProgramReviewWindow();
			}
		});
		liscenceButton = new JButton("Liscencing");
		liscenceButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				q.viewLiscencingInfo();
				WindowGenerator.generateLiscencesWindow();
			}
		});
		developerButton = new JButton("Developer Info");
		developerButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				q.infoWindowDevInfo();
				WindowGenerator.generateDeveloperWindow();
			}
		});
		downloadButton = new JButton("Download");
		
		//if download exists show download, else disable button
		q.testDownloadAvailable();
		if(!Query.testDownloadAvailable.equals("0")){
			downloadButton.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					//set values for the downloads window and open it
					q.searchWindowDownload();				
					WindowGenerator.generateDownloadWindow();
				}
			});
		} else {
			downloadButton.setEnabled(false);
		}

		writeReviewButton = new JButton("Write Review");
		writeReviewButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				WindowGenerator.generateWriteReviewWindow();
			}
		});
		buttonPanel.add(reviewButton);
		buttonPanel.add(liscenceButton);
		buttonPanel.add(developerButton);
		buttonPanel.add(downloadButton);
		buttonPanel.add(writeReviewButton);
		
		add(buttonPanel, BorderLayout.SOUTH);
	}	
}
